<?php defined('SYSPATH') or die('No direct script access.');

class Model_Audi
{
	public function __construct()
	{
	}
	
	public function get_types_of_body()
	{
		$query = DB::select()->from('types_of_body');
		$result = $query->execute()->as_array();
				
		return $result;
	}
	
	public function get_categories()
	{
		$categories = array();
		$query = DB::select()->from('categories')->where('parent_id','=','0');
		$result = $query->execute()->as_array();
		
		for($i=0;$i<sizeof($result);$i++)
		{
			$subcategories = array();
			$subquery = DB::select()->from('categories')->where('parent_id','=',$result[$i]['category_id']);
			$subresult = $subquery->execute()->as_array();
			
			$categories[$i]['category_id'] = $result[$i]['category_id'];
			$categories[$i]['name'] = $result[$i]['name'];
			$categories[$i]['image'] = $result[$i]['image'];
			$categories[$i]['description'] = $result[$i]['description'];
			
			for($j=0;$j<sizeof($subresult);$j++)
			{
				$subcategories[$j]['category_id'] = $subresult[$j]['category_id'];
				$subcategories[$j]['name'] = $subresult[$j]['name'];					
				$subcategories[$j]['image'] = $subresult[$j]['image'];
				$subcategories[$j]['description'] = $subresult[$j]['description'];
			}
			
			$categories[$i]['subcategories'] = $subcategories;
		}
		
		return $categories;
	}
	
	public function get_category_id($model)
	{
		$query = DB::select('category_id')->from('categories')->where('name', 'LIKE', $model);
		$category = $query->execute()->as_array();
		
		return $category[0]['category_id'];
	}
	
	public function get_subcategory_id($model, $generation)
	{
		$query = DB::query(Database::SELECT, 'SELECT category_id
											  FROM categories
											  WHERE name LIKE :generation
												AND parent_id = (SELECT category_id
																 FROM categories
																 WHERE name LIKE :model)');
		$subcategory = $query->param(':generation', $generation)->param(':model', $model)->execute()->as_array();
		
		return $subcategory[0]['category_id'];
	}
	
	public function get_generations($category_id)
	{
		$generations = array();
		$query       = DB::select()->from('categories')->where('parent_id', '=', $category_id);
		$generations = $query->execute()->as_array();
		
		return $generations;
	}
	
	public function get_images($category_id, $start, $count)
	{
		$images = array();
		$query  = DB::select()->from('images')->where('category_id', '=', $category_id)->limit($count)->offset($start);
		$images = $query->execute()->as_array();
		
		return $images;
	}
	
	public function get_description($category_id)
	{
		$query = DB::select('description')->from('categories')->where('category_id', '=', $category_id);
		$category = $query->execute()->as_array();
		
		return $category[0]['description'];
	}
	
	public function get_pages($category_id, $count, $page)
	{
		$query = DB::select('image_id')->from('images')->where('category_id', '=', $category_id);
		$num = $query->execute()->count();
		
		$num_pages = ceil($num/$count);
		
		$pages = array();
		for($i = 1; $i <= $num_pages; $i++)
		{
			if($i == $page)
				$pages[$i]['url'] = '#';
			else if($i == 1)
				$pages[$i]['url'] = '';
			else
				$pages[$i]['url'] = 'page'.$i;
				
			$pages[$i]['anchor'] = $i;
		}
		
		return $pages;
	}
	
	public function get_image($image_id)
	{
		$query = DB::select()->from('images')->where('image_id', '=', $image_id);
		$images = $query->execute()->as_array();
		
		return $images[0];
	}
	
	public function get_near_images($image_id, $near)
	{	
		$query = DB::query(Database::SELECT, 'SELECT *
											  FROM images
											  WHERE image_id BETWEEN :left AND :right
												AND image_id != :image_id
												AND category_id = (SELECT category_id
																		 FROM images
																		 WHERE image_id = :image_id)');
		$images = $query->param(':left', $image_id - $near)->param(':right', $image_id + $near)->
						  param(':image_id', $image_id)->execute()->as_array();
		
		return $images;
	}
}